<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\PushKsReportRecordAccountModel;
use app\model\PushKsReportRecordCampaignModel;
use app\model\PushKsReportRecordUnitModel;
use app\model\PushOeReportRecordAccountModel;
use app\model\PushOeReportRecordCampaignModel;
use app\model\PushOeReportRecordUnitModel;
use think\console\Input;
use think\console\Output;

class ReportRecordDeleteListWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('push_report_record_delete_list_work');
        $this->setDescription('删除七天后报表数据日报时报队列');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======删除七天后报表数据日报时报队列开始' . date('Y-m-d H:i:s', time()) . '=======');
        try {
            $date = date('Ymd', strtotime('-5 days')); // 五天前的时间
            $res = PushKsReportRecordAccountModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======ks_account删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }
            $res = PushKsReportRecordCampaignModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======ks_campaign删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }
            $res = PushKsReportRecordUnitModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======ks_unit删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }
            $res = PushOeReportRecordAccountModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======oe_account删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }
            $res = PushOeReportRecordUnitModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======oe_campaign删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }
            $res = PushOeReportRecordCampaignModel::where('stat_date', '<', $date)->delete();
            if ($res) {
                $output->writeln('=======oe_unit删除成功' . date('Y-m-d H:i:s', time()) . '=======');
            }

        } catch (\Exception $e) {
            $output->writeln('=======删除七天后报表数据日报时报队列失败' . $e->getMessage() . date('Y-m-d H:i:s', time()) . '=======');
        }
        $output->writeln('=======删除七天后报表数据日报时报队列结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
